/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package makao;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import shared.Card;

public class CardsPanel extends JPanel
{

    private static final int HEIGHT = 170;
    private MakaoView makaoView = null;
    private Boolean paintableFlag = false;
    private Graphics2D graph2D;
    private PlayerData playerData;
    private ArrayList<Point> cardPosition = new ArrayList<Point>();
    private TablePanel table;

    @Override
    public void paint(Graphics g)
    {
        graph2D = (Graphics2D) g;
        super.paintComponent(graph2D);

        if (paintableFlag)
        {
            if (playerData.getMyCards().size() != cardPosition.size())
            {
                revalidiateCardsPositions(playerData.getMyCards());
            }

            for (int i = playerData.getMyCards().size() - 1; i >= 0; i--)
            {
                graph2D.drawImage((new ImageIcon("src/images/" + getImagePackage(playerData.getMyCards().get(i).getSuit())
                        + "/" + getImageFileName(playerData.getMyCards().get(i).getType()) + ".png")).getImage(),
                        cardPosition.get(i).x, cardPosition.get(i).y, this);
            }

            this.revalidiatePanelSize();
        }

    }

    public void revalidiateCardsPositions(ArrayList<Card> userCards)
    {
        cardPosition.clear();
        cardPosition.add(new Point(0, 20));

        for (int i = 1; i < userCards.size(); i++)
        {
            cardPosition.add(new Point(70 * i, 20));
        }
    }

    public void revalidiatePanelSize()
    {
        this.setPreferredSize(new Dimension((playerData.getMyCards().size() - 1) * 70 + 93, HEIGHT));
        this.revalidate();
    }

    public void chooseCard(int x, int y)
    {
        int cardNumber = checkCardClicked(x, y);

        if (cardNumber == -1)
        {
            return;
        } else
        {
            if (makaoView.getCardCacheFrame().isVisible() == false)
            {
                makaoView.getCardCacheFrame().setLocation(makaoView.getFrame().getLocationOnScreen().x + (makaoView.getFrame().getWidth() / 2 - 334),
                        makaoView.getCardsPanel().getLocationOnScreen().y - 223);
            }

            makaoView.getTablePanel().setEnabled(false);
            if( playerData.getMyCards().get(cardNumber).getType() != Card.JOKER )
            {
                makaoView.getCardCacheFrame().setVisible(true);
                makaoView.getCardCacheFrame().getCardsCachePanel().addCard(new Card(playerData.getMyCards().get(cardNumber).getSuit(),
                        playerData.getMyCards().get(cardNumber).getType()));
            }
            else
            {
                makaoView.getCardCacheFrame().setEnabled(false);
                makaoView.getCardsPanel().setEnabled(false);
                makaoView.getJokerReplaceForm().setLocation( makaoView.getFrame().getLocationOnScreen().x + (makaoView.getFrame().getWidth() / 2 - 310), 
                                                             makaoView.getCardsPanel().getLocationOnScreen().y - 260);
                makaoView.getJokerReplaceForm().setVisible(true);
            }
            playerData.removeCard(cardNumber);
            this.repaint();
        }
    }
    
    public void chooseCardAsJocker(int suit, int type)
    {
        makaoView.getCardCacheFrame().setVisible(true);
        makaoView.getCardCacheFrame().getCardsCachePanel().addCard(new Card(Card.NONE_SUIT, Card.JOKER, new Card(suit, type))); 
        makaoView.getCardCacheFrame().setEnabled(true);
        makaoView.getCardsPanel().setEnabled(true);
    }

    public void undeleteCards(ArrayList<Card> cardsArray)
    {
        if (cardsArray != null)
        {
            for (Card card : cardsArray)
            {
                playerData.getMyCardsToUndelete().add(new Card(card.getSuit(), card.getType()));
            }
        }
    }

    public void selectCard(int x, int y)
    {
        int cardNumber = checkCardClicked(x, y);

        if (cardNumber == -1)
        {
            return;
        } else
        {
            for (int i = 0; i < cardPosition.size(); i++)
            {
                if (i == cardNumber)
                {
                    if (cardPosition.get(i).y != 0)
                    {
                        cardPosition.get(i).move(cardPosition.get(i).x, 0);
                    } else
                    {
                        cardPosition.get(i).move(cardPosition.get(i).x, 20);
                    }
                } else
                {
                    cardPosition.get(i).move(cardPosition.get(i).x, 20);
                }
            }
        }

        this.repaint();
    }

    public int checkCardClicked(int x, int y)
    {
        if ((x > (cardPosition.get(0).x)) && (x < (cardPosition.get(0).x + 93))
                && (y > cardPosition.get(0).y))
        {
            return 0;
        }

        for (int i = 1; i < playerData.getMyCards().size(); i++)
        {
            if ((x > (cardPosition.get(i).x + 23)) && (x < (cardPosition.get(i).x + 93))
                    && (y > cardPosition.get(i).y))
            {
                return i;
            }
        }

        return -1;
    }

    private String getImagePackage(int cardSuit)
    {
        switch (cardSuit)
        {
            case Card.NONE_SUIT:
                return "extra";
            case Card.HEART:
                return "heart";
            case Card.DIAMOND:
                return "diamond";
            case Card.CLUB:
                return "club";
            case Card.SPADE:
                return "spade";
        }
        return null;
    }

    private String getImageFileName(int cardType)
    {
        if (cardType <= Card.TEN)
        {
            return Integer.toString(cardType);
        } else
        {
            switch (cardType)
            {
                case Card.ACE:
                    return "A";
                case Card.KING:
                    return "K";
                case Card.QUEEN:
                    return "Q";
                case Card.JACK:
                    return "J";
                case Card.JOKER:
                    return "jocker";
            }
        }
        return null;
    }

    public void setPlayerData(PlayerData playerData, MakaoView makaoView)
    {
        this.makaoView = makaoView;
        this.playerData = playerData;
        paintableFlag = true;
    }

    public void setTable(TablePanel table)
    {
        this.table = table;
    }
}
